package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseActivity;
import com.me.common.base.Const;
import com.me.common.base.OttoBus;
import com.me.jingdongshop.R;
import com.me.jingdongshop.ui.fragment.CategoryFragment;
import com.me.jingdongshop.ui.fragment.DiscoverFragment;
import com.me.jingdongshop.ui.fragment.HomeFragment;
import com.me.jingdongshop.ui.fragment.MyFragment;
import com.me.jingdongshop.ui.fragment.ShopCartFragment;
import com.me.jingdongshop.ui.view.FloatImage;
import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private FrameLayout contentFrame;
    private FragmentTabHost tabHost;
    private String texts[] = {"首页", "分类", "发现", "购物车", "我的"};
    private int imageButton[] = {R.drawable.tab_home_selector,
            R.drawable.tab_category_selector,
            R.drawable.tab_setting_selector,
            R.drawable.tab_shopcart_selector,
            R.drawable.tab_my_selector};
    private List<Class> fragmentArray = new ArrayList<>();
    private FloatImage floatImage;
    private WindowManager mWindowManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册Otto事件
        OttoBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OttoBus.getDefault().unregister(this);
    }

    /**
     * 定义接收Otto事件的方法
     */
    @Subscribe
    public void onEvent(Message msg) {
        //如果是登录了或注销了，需要重新根据登录状态刷新当前界面
        if (msg.what == Const.TOCART) {
            int i = Integer.parseInt((String) msg.obj);
            System.out.println(Thread.currentThread().getName());
           /* if(tabHost != null) {
                tabHost.setCurrentTab(i);
            }*/
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int key = intent.getIntExtra("key", -1);
        if(key == 3){
            tabHost.setCurrentTab(key);
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    public Fragment getFragment() {
        String tag = tabHost.getCurrentTabTag();
        return getSupportFragmentManager().findFragmentByTag(tag);
    }

    @Override
    public void initView() {

        contentFrame = (FrameLayout) findViewById(R.id.content_frame);
        tabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        tabHost.setup(this, getSupportFragmentManager(), R.id.content_frame);

        fragmentArray.add(HomeFragment.class);
        fragmentArray.add(CategoryFragment.class);
        fragmentArray.add(DiscoverFragment.class);
        fragmentArray.add(ShopCartFragment.class);
        fragmentArray.add(MyFragment.class);
        for (int i = 0; i < texts.length; i++) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec("news" + i);
            View v = getIndicatorView(texts[i], imageButton[i]);
            tabSpec.setIndicator(v);
            tabHost.addTab(tabSpec, fragmentArray.get(i), null);
        }
    }

    private void initFloatImage() {
        if (floatImage == null) {
            mWindowManager = getWindowManager();
            int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
            int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
            floatImage = new FloatImage(this);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            params.x = screenWidth;
            params.y = screenHeight / 2;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.LEFT | Gravity.TOP;
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
            params.format = PixelFormat.RGBA_8888;
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            floatImage.setLayoutParams(params);
            mWindowManager.addView(floatImage, params);

            floatImage.setFloatImageListener(new FloatImage.FloatImageListener() {
                @Override
                public void onClick() {
                    Toast.makeText(MainActivity.this, "点我点我", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(getApplicationContext())) {
                //启动Activity让用户授权
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivity(intent);
                return;
            } else {
                initFloatImage();
            }
        } else {
            initFloatImage();
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mWindowManager != null && floatImage != null) {
            mWindowManager.removeView(floatImage);
            floatImage = null;
        }
    }

    @Override
    public void initListener() {
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {

    }

    private View getIndicatorView(String name, int imgId) {
        View indicatorView = View.inflate(this, R.layout.indicator_item, null);
        ImageView img = (ImageView) indicatorView.findViewById(R.id.indicator_img);
        TextView tv = (TextView) indicatorView.findViewById(R.id.indicator_tv);
        tv.setText(name);
        img.setBackgroundResource(imgId);
        return indicatorView;
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {

    }

    @Override
    public void onHttpError(int reqType, String error) {

    }

    public void setTab(int i) {
        if (tabHost != null) {
            tabHost.setCurrentTab(i);
        }
    }
}
